package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.ViewGroup;

import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.FireWareUpdateActivity;
import com.proton.carepatchtemp.adapterholder.DeviceHolder;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.Device;
import com.proton.carepatchtemp.utils.RvUtils;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by Api on 2016/10/9.
 */

public class DeviceAdapter extends BaseAdapter<DeviceHolder> {

    private Context mContext;

    public DeviceAdapter(List datas, Context context) {
        super(datas);
        mContext = context;
    }

    @Override
    public DeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DeviceHolder(RvUtils.getMyView(R.layout.item_find_devices, parent));
    }

    @Override
    public void onBindViewHolder(DeviceHolder holder, int position) {
        try {
            Device mDevice = (Device) datas.get(position);
            LogUtils.e(mDevice);

//            if (AppUtils.compareVersion(mDevice.getVersion(), "1.0.2") >= 0) {
//                if (null != VCare.get().getFirmWare() && AppUtils.compareVersion(mDevice.getVersion(), VCare.get().getFirmWare().getVersion()) < 0) {
//                    needUpdate = true;
//                    BadgeView badge = new BadgeView(DeviceDetialActivity.this);
//                    badge.setBadgeMargin(0, 0, 32, 0);
//                    badge.setTargetView(mTvFirmwareNeedUpdate);
//                    badge.setBadgeCount(1);
//                    bas_in = new BounceTopEnter();
//                    bas_out = new SlideBottomExit();
//                }
//            }

            if (mDevice.getBtaddress().equalsIgnoreCase("0A:D0:AD:0A:D0:AD")) {
                holder.mConnect.setText(R.string.click_updata);
                holder.mConnect.setOnClickListener(view -> {
                    Intent intent = new Intent(mContext, FireWareUpdateActivity.class);
                    intent.putExtra(Extras.DEVICE_UPDATE_KEY, "Update");
                    mContext.startActivity(intent);
                });
            }
//            else if ((AppUtils.compareVersion(mDevice.getVersion(), "1.0.2") >= 0
//                    && null != VCare.get().getFirmWare()
//                    && AppUtils.compareVersion(mDevice.getVersion(), VCare.get().getFirmWare().getVersion()) < 0)) {
//                holder.mConnect.setText(R.string.click_updata);
//
//                holder.mConnect.setOnClickListener(view -> {
//                    EventBus.getDefault().post(new MainEvent(Codes.PRF_GO_CHOOSE_PRF, mDevice));
//                });
//
//                SweetAlertDialog dialog = new SweetAlertDialog(mContext, Type.NORMAL_TYPE);
//                dialog.setCancelable(false);
//                dialog.setTitleText("发现新版本固件");
//                dialog.setContentText("建议及时升级！");
//                dialog.showCancelButton(true);
//                dialog.setConfirmText("升级");
//                dialog.setCancelText("取消");
//                dialog.setCancelClickListener(Dialog::dismiss);
//                dialog.setConfirmClickListener(sweetAlertDialog -> {
//                    Intent intent = new Intent(mContext, FireWareUpdateActivity.class);
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable(Extras.DEVICE_DETIAL, mDevice);
//                    intent.putExtras(bundle);
//                    mContext.startActivity(intent);
//                });
//            }
            else {
                holder.mConnect.setText(R.string.device_click);
                holder.mConnect.setOnClickListener(view -> {
                    EventBus.getDefault().post(new MainEvent(Codes.PRF_GO_CHOOSE_PRF, mDevice));
                });
            }

            String str = VCare.get().getString(R.string.tiwen) + mDevice.getBtaddress().substring(12);
            String[] strs = str.split("_");
            holder.mDeviceName.setText(strs[0]);
            holder.getmDeviceNameUpper.setText(strs[1]);

        } catch (Throwable e) {
            LogUtils.e(e);
        }
    }
}